<template>
  <div class="app-container">
    <div class="zd3">
      <div class="zd3-title">
        <p>
          <span>部门</span>
          <input v-model="bumen" type="text" placeholder="请输入部门" />
        </p>
        <h3 @click="getList">查询</h3>
        <h4 @click="resetList">重置</h4>
				<h3 @click="addPeople" style="background: #0093FF">新增</h3>
      </div>
      <div class="zd3-btm">
        <!-- <div class="zd3-btm-title">
          <p style="flex: 1;">部门名称</p>
					<p style="flex: 1;">董事长审批金额</p>
					<p style="flex: 1;">总经理审批金额</p>
					<p style="flex: 1;">总监审批金额</p>
					<p style="flex: 1;">部长审批</p>
          <p style="width: 250px;">操作</p>
        </div>
        <div class="addDate" @click="addPeople">+ 新增审批规则</div> -->
        <el-table
          :data="tableData"
          style="width: 100%;margin-bottom: 20px;"
          v-loading="loading"
        >
          <el-table-column align="center" prop="departments" label="部门名称"></el-table-column>
          <!-- <el-table-column align="center" prop="four" label="董事长审批金额"></el-table-column> -->
          <el-table-column align="center" prop="four" label="总经理审批金额"></el-table-column>
          <el-table-column align="center" prop="three" label="总监审批金额"></el-table-column>
          <el-table-column align="center" prop="two" label="部长审批"></el-table-column>
          <el-table-column align="center" width="250" label="操作">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="write(scope.row,scope.$index)">修改</el-button>
              <el-popconfirm
                placement="top"
                hideIcon
                title="是否要删除此行？"
                @onConfirm="deleteRow(scope.row)"
              >
                <el-button type="text" size="small" slot="reference">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          hide-on-single-page
          layout="prev, pager, next"
          :total="total"
          @prev-click="goPrev"
          @next-click="goNext"
        />
      </div>
    </div>
    <el-dialog :visible.sync="dialogVisible" :modal-append-to-body="false" :modal="true" width="60%">
      <div class="change">
        <p>
          <span>部门名称</span>
          <el-select v-model="tempData.departments" placeholder="请选择部门" class="userselect">
            <el-option
              v-for="item in ifydata"
              :key="item.id"
              :label="item.departments"
              :value="item.departments"
            ></el-option>
          </el-select>
        </p>
				<!-- <p>
				  <span>部长-达到审批条件金额</span>
				  <input type="text" v-model="tempData.departments" placeholder="请输入达到审批条件金额" />
				</p> -->
				<p>
				  <span>部长 - 最大审批金额</span>
				  <input type="text" v-model="tempData.ruleTwoCondition" placeholder="请输入最大审批金额" />
				</p>
				<p>
          <span>总监 - 最大审批金额</span>
          <input type="text" v-model="tempData.ruleThreeCondition" placeholder="请输入最大审批金额" />
        </p>
				<p>
				  <span>总经理 - 最大审批金额</span>
				  <input type="text" v-model="tempData.ruleFourCondition" placeholder="请输入最大审批金额" />
				</p>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消保存</el-button>
        <el-button type="primary" @click="save">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getBumen } from '@/api/people'
import { getShenpi,updateShenpi,addShenpi,deleteShenpi } from '@/api/home'
export default {
  data() {
    return {
      name: "",
      bumen: "",
      total: 1,
      showHeader: false,
      tableData: [
        {
          id: 1,
          date: "2016-05-02",
          bumen: "销售部",
          bumencode: "001",
          name: "王大虎",
          status: 1,
          phone: "15032154252",
          ischeck: false
        },
      ],
			ifydata: [],   //部门列表
      dialogVisible: false,
      tempData: {},
      tempIndex: -1,
      loading: true
    };
  },
  mounted(){
		this.bumenList()
    // this.getList()
  },
  methods: {
    changeValue3(value) {},
    getList() {	
      const param = {
        departments: this.bumen || null
      }
      getShenpi(param).then(res => {
        console.log('部门管理')
        console.log(res)
				res.data.map(item => {
					item.four = this.fmoney(item.ruleFourCondition,2)
					item.three = this.fmoney(item.ruleThreeCondition,2)
					item.two = this.fmoney(item.ruleTwoCondition,2)
					return item
				})
        this.tableData = res.data
        this.loading = false
      })
    },
		// 部门列表
		bumenList(){
		  getBumen({}).then(res => {
		    console.log(res)
		    res.data.map(item => {
					item.value = item.departments
					return item
				})
				this.ifydata = [...res.data]
		  })
		},
    write(item, index) {
			this.tempData = {...item}
			this.dialogVisible = true
			this.tempIndex = index
		},
    save() {
			if(this.tempIndex == -1){
        addShenpi(this.tempData).then(res => {
          console.log(res)
          this.$message({
            showClose: true,
            message: res.message,
            type: 'success'
          });
          this.getList()
        })
			}else{
        updateShenpi(this.tempData).then(res => {
          console.log(res)
          this.$message({
            showClose: true,
            message: res.message,
            type: 'success'
          });
          this.getList()
        })
      }
      this.dialogVisible = false
			this.tempIndex = -1
		},
    deleteRow(item) {
      console.log("删除");
      deleteShenpi(item.ruleId).then(res => {
        console.log(res)
        this.$message({
          showClose: true,
          message: res.message,
          type: 'success'
        });
        this.getList()
      })
    },
		// 数字转换成金额
		fmoney(s, n) {
			n = n > 0 && n <= 20 ? n : 2;  
			s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";  
			let l = s.split(".")[0].split("").reverse(), r = s.split(".")[1];  
			let t = "";
			for (let i = 0; i < l.length; i++) {
				t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");  
			}
			return t.split("").reverse().join("") + "." + r;  
		}, 
		rmoney(s) {
		  return parseFloat(s.replace(/[^\d\.-]/g, ""));  
		},
    goPrev(page) {
      console.log(page);
    },
    goNext(page) {
      console.log(page);
    },
    resetList() {
      this.bumen = ""
    },
    addPeople() {
      this.dialogVisible = true;
			this.tempIndex = -1;
			this.tempData = {
				"departments": '',
				"ruleFour": "董事长",
				"ruleFourCondition": '',
				"ruleOne": "部长",
				"ruleThree": "总经理",
				"ruleThreeCondition": '',
				"ruleTwo": "总监",
				"ruleTwoCondition": '',
				"ruleType": "1"
			}
    }
  }
};
</script>

<style lang="scss" scoped>
/deep/.el-button--primary {
  color: #fff;
  background-color: #18a05e !important;
  border-color: #18a05e !important;
}
/deep/.el-button--primary:hover,
.el-button--primary:focus {
  background: #18a05e !important;
  border-color: #18a05e !important;
  color: #fff !important;
}
/deep/.el-button:hover,
.el-button:focus {
  color: #18a05e;
  background-color: transparent;
}
/deep/.el-button--primary {
  color: #fff;
  background-color: #18a05e;
  border-color: #18a05e;
}
/deep/.el-button--text {
  color: #00c666 !important;
}

.app-container {
  padding: 10px;
}
.status {
  border-radius: 3px;
  font-size: 14px;
  width: 50px;
  color: #fff;
  background: #777777;
  margin: 0 auto;
}
.status-active {
  background: #18a05e;
}
.inps {
  width: 100%;
  height: 32px;
  line-height: 32px;
  padding-left: 10px;
  background: none;
  border: none;
  color: #333;
}
.inps-active {
  width: 100%;
  height: 32px;
  line-height: 32px;
  padding-left: 10px;
  // background:#ccc;
  border: none;
  color: #333;
}
.zd3-title {
	height: 80px;
	display: flex;
	align-items: center;
	padding: 0 20px;
	position: relative;
	background: #ffffff;
	border-radius: 8px;
	border: 1px solid rgba(0, 0, 0, 0.1);

	> p {
		font-size: 14px;
		color: #333;
		margin-right: 30px;

		> span {
			padding-right: 20px;
		}

		> input {
			width: 200px;
			height: 32px;
			// background: #ccc;
			border-radius: 3px;
			border: 1px solid #ccc;
			padding-left: 10px;
			color: #333;
		}
	}

	> h3 {
		width: 64px;
		height: 32px;
		background: #18a05e;
		border-radius: 3px;
		margin: 0 30px 0 0;
		color: #fff;
		font-size: 14px;
		line-height: 32px;
		font-weight: 100;
		text-align: center;
		cursor: pointer;
	}

	> h4 {
		width: 64px;
		height: 32px;
		border-radius: 3px;
		border: 1px solid rgba(0, 0, 0, 0.1);
		color: #333;
		font-size: 14px;
		line-height: 32px;
		font-weight: 100;
		text-align: center;
		cursor: pointer;
		margin-right: 30px;
	}
}
.zd3-btm .btn-prev:after {
  content: "上一页";
}
.zd3-btm .btn-next:before {
  content: "下一页";
}
.zd3-btm .btn-prev,
.zd3-btm .btn-next {
  width: 65px;
}
.zd3-btm {
  background: rgba(255, 255, 255, 1);
  border-radius: 8px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  margin-top: 10px;
  overflow: hidden;
  &-title {
    display: flex;
    align-items: center;
    height: 70px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    background: #ffffff;
    > p {
      text-align: center;
      font-size: 16px;
      color: #333;
    }
  }
  .addDate {
    margin: 20px 32px 10px 32px;
    height: 32px;
    border-radius: 3px;
    text-align: center;
    line-height: 32px;
    font-size: 14px;
    color: #333;
    cursor: pointer;
    border: 1px dashed rgba(0, 0, 0, 0.3);
  }
  .el-button--text {
    margin: 0 15px;
  }
}
.change {
  display: flex;
  flex-wrap: wrap;
	justify-content: space-between;
  > p {
    width: 48%;
    font-size: 14px;
		height: 56px;
    display: flex;
		flex-direction: column;
    // align-items: center;
    color: #333;
    /deep/.el-input__inner {
      width: inherit;
      background: none;
      height: 32px;
      // line-height: 32px;
      color: #333;
      border: 1px solid rgba(0, 0, 0, 0.1);
      padding: 0 25px;
    }
    /deep/.el-input__icon {
      line-height: 32px;
    }
    .userselect {
      flex: 1;
    }

    > span {
      display: block;
      // width: 120px;
      // text-align: right;
			color: #18a05e;
			font-weight: 600;
      padding-bottom: 8px;
    }
    > input {
      // width: inherit;
      flex: 1;
      height: 32px;
      border-radius: 3px;
      border: 1px solid rgba(0, 0, 0, 0.1);
      padding-left: 10px;
      color: #333;
    }
  }
}
</style>
